//统计类
package v1

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"math/rand"
	"strconv"
	"time"
	"zimuzu_cvn_web_api/config"
	"zimuzu_cvn_web_api/model"
	"zimuzu_cvn_web_api/pkg/e"
	"zimuzu_cvn_web_api/pkg/errno"
	"zimuzu_cvn_web_api/router/api"
	"zimuzu_cvn_web_api/router/api/admin/middleware"
)

type StatisticsAPI struct {
}

func (statistics StatisticsAPI) Name() string {
	return "statistics API"
}

func (statistics StatisticsAPI) Bind() {
	statisticsAPI := config.C.Router.Group("/v1/admin/statistics", middleware.TrackLog(), middleware.AuthMiddleware())
	{
		statisticsAPI.GET("unExamine", unExamine)
		statisticsAPI.GET("cvntDayly", cvntDayly)
		statisticsAPI.GET("usdtDayly", usdtDayly)
		statisticsAPI.GET("cvnRechargeDayly", cvnRechargeDayly)
		statisticsAPI.GET("usdtRechargeDayly", usdtRechargeDayly)
		statisticsAPI.GET("cdnProfit", cdnProfit)
		statisticsAPI.GET("globalList", globalList)
		statisticsAPI.GET("globalGbps", globalGbps)
		statisticsAPI.GET("globalNodeInfo", globalNodeInfo)
	}
}

type unExamineResponse struct {
	CvntUnExamineTotal float64 `json:"cvntUnExamineTotal"`
	UsdtUnExamineTotal float64 `json:"usdtUnExamineTotal"`
	CvnWthdrawTotal    float64 `json:"cvnWthdrawTotal"`
	CvnExamineTotal    float64 `json:"cvnExamineTotal"`
	UsdtWthdrawTotal   float64 `json:"usdtWthdrawTotal"`
	UsdtExamineTotal   float64 `json:"usdtExamineTotal"`
}

//查询待审核的CVNT&USDT金额
func unExamine(c *gin.Context) {
	var response unExamineResponse

	response.CvntUnExamineTotal, _ = model.GetSumUnexamineAmount()
	response.UsdtUnExamineTotal, _ = model.GetSumUsdtUnexamineAmount()
	response.CvnWthdrawTotal, _ = model.GetWithdrawTotalCvn()
	response.CvnExamineTotal, _ = model.GetSumEexamineAmount()
	response.UsdtWthdrawTotal, _ = model.GetWithdrawTotalUsdt()
	response.UsdtExamineTotal, _ = model.GetSumUsdtExamineAmount()

	errno.OK.Info = "ok"
	api.SendResponse(c, errno.OK, response)
	return
}

type DaylyParams struct {
	Page int `json:"page"  form:"page"`
	Per  int `json:"per"  form:"per"`
}

//cvnt每日充提信息
//Auth:Xu
func cvntDayly(c *gin.Context) {
	var params DaylyParams
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	if params.Page == 0 {
		params.Page = 1
	} else {
	}
	if params.Per == 0 {
		params.Per = 30
	}
	res, err := model.CvnLogAdminStatistjcs(params.Page, params.Per)

	errno.OK.Info = "ok"
	api.SendResponse(c, errno.OK, res)
	return
}

//cvnt每日充提信息
//Auth:Xu
func usdtDayly(c *gin.Context) {
	var params DaylyParams
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	if params.Page == 0 {
		params.Page = 1
	} else {
	}
	if params.Per == 0 {
		params.Per = 30
	}
	res, err := model.UsdtLogAdminStatistjcs(params.Page, params.Per)

	errno.OK.Info = "ok"
	api.SendResponse(c, errno.OK, res)
	return
}

//cvn每日充值信息
//Auth:Xu
func cvnRechargeDayly(c *gin.Context) {
	var params DaylyParams
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	if params.Page == 0 {
		params.Page = 1
	} else {
	}
	if params.Per == 0 {
		params.Per = 30
	}
	//response := make(map[string]float64,0)
	res, err := model.CvnLogRechargeDayly(params.Page, params.Per)
	//for _, day := range res{
	//	response[day.Date] = day.Amount
	//}

	errno.OK.Info = "ok"
	api.SendResponse(c, errno.OK, res)
	return
}

//cvn每日充值信息
//Auth:Xu
func usdtRechargeDayly(c *gin.Context) {
	var params DaylyParams
	err := c.ShouldBindWith(&params, binding.Query)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}
	if params.Page == 0 {
		params.Page = 1
	} else {
	}
	if params.Per == 0 {
		params.Per = 30
	}
	//response := make(map[string]float64,0)
	res, err := model.UsdtLogRechargeDayly(params.Page, params.Per)
	//for _, day := range res{
	//	response[day.Date] = day.Amount
	//}

	errno.OK.Info = "ok"
	api.SendResponse(c, errno.OK, res)
	return
}

// 全网节点信息
// Auth: hzp
func globalNodeInfo(c *gin.Context) {

	typeStr := c.Query("type")
	typeInt, _ := strconv.Atoi(typeStr)
	_, dailyData, err := model.GetDailyBoardData(1, 7)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}

	var temp model.DateValueStruct
	var res []model.DateValueStruct
	for _, k := range dailyData {
		temp.Date = k.Date
		if typeInt == 1 {
			temp.Value, _ = strconv.ParseFloat(strconv.Itoa(k.NodeNums), 64)
		} else if typeInt == 2 {
			temp.Value = k.PledgeCvn
			temp.Value2 = k.UnPledgeCvn
		} else if typeInt == 3 {
			// 单位KB
			temp.Value = k.Storage
		}
		res = append(res, temp)
	}

	errno.OK.Info = "ok"
	api.SendResponse(c, errno.OK, res)
	return
}

// 全网带宽数据
// Auth: hzp
func globalGbps(c *gin.Context) {

	var res model.GlobalGbpsStruct
	timeNowStr := time.Now().Format(e.TimeFormatD)
	start := e.StrToTime(timeNowStr + " 00:00:00")
	end := time.Now().Unix()
	gbps, err := model.GetTheDateGbps(start, end, 0)
	if err != nil {
		api.SendResponse(c, err, res)
		return
	}
	res.Gbps = gbps
	res.Speed = e.SizeFormat(gbps*3600*1024*1024*1024/8, 2) + "/h"

	errno.OK.Info = "ok"
	api.SendResponse(c, errno.OK, res)
	return
}

// 全网每天数据
// Auth: hzp
func globalList(c *gin.Context) {

	var params model.PageParams
	if params.Page < 1 {
		params = model.NewDefaultPageParams()
	}
	err := c.ShouldBindQuery(&params)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}

	count, dailyData, err := model.GetDailyBoardData(params.Page, params.Per)
	if err != nil {
		api.SendResponse(c, errno.ParamsError, nil)
		return
	}
	var temp model.GlobalList
	var res []model.GlobalList
	for _, k := range dailyData {
		temp.Date = k.Date
		temp.New = k.CreateNums
		temp.MiningCvn = k.OutPut
		temp.Online = k.OnlineNums
		temp.Storage = k.Storage
		temp.Pledge = k.PledgeCvn
		temp.UnPledge = k.UnPledgeCvn
		res = append(res, temp)
	}

	errno.OK.Info = strconv.Itoa(count)
	api.SendResponse(c, errno.OK, res)
	return
}

// 全网每天数据
// Auth: hzp
func cdnProfit(c *gin.Context) {

	var temp model.CdnProfitStruct
	var res []model.CdnProfitStruct
	for i := 7; i > 0; i-- {
		temp.Month = time.Now().AddDate(0, -i, 0).Month().String()
		temp.Profit1 = e.Decimal(float64(288*i*e.RandInt(1, 5))-rand.Float64(), 2)
		temp.Profit2 = e.Decimal(float64(1999*i*e.RandInt(1, 3))-rand.Float64(), 2)
		res = append(res, temp)
	}

	errno.OK.Info = "ok"
	api.SendResponse(c, errno.OK, res)
	return
}
